﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Lswweb.Domain;
using Lswweb.Model;

namespace Lswweb.Controls
{
    /// <summary>
    /// 单文件上传控件，支持文大小验证，扩展名验证，自动生成上传名称等功能。 注：文件上传到临时文件夹。
    /// </summary>
    public class FileUpload : System.Web.UI.WebControls.FileUpload
    {
        private string _uploadDirectory =SiteSettings.Settings.UploadTempDirectory;
        private int _upfileSize = SiteSettings.Settings.UpFileMaxSize;
        private string _fileExtensions = string.Empty;

        /// <summary>
        /// 根据指定路径上传文件，如果使用此方法上传文件，那么AutoCreateFileName和UploadDirectory属性则失效。
        /// </summary>
        /// <param name="fileName">字符串，一个指定服务器上用于保存上传文件的完整路径或url。</param>]
        /// <returns>返回文件url，如果文件服务器上已经存在则会重命名并返回新名称的url</returns>
        public virtual new string SaveAs(string fileName)
        {
            if (!HasFile)
                return string.Empty;

            if (FileSize > MaxFileSize)
            {
                SiteContext.RenderMessage(ExceptionType.FileTooBig);
                return string.Empty;
            }

            if (!Globals.ValidateFileExtension(FileName, FileExtensions))
            {
                SiteContext.RenderMessage(ExceptionType.FileExtensionError);
                return string.Empty;
            }

            fileName = CheckFileHas(fileName);
            base.SaveAs(Globals.GetFilePath(fileName));

            return fileName;
        }

        public int FileSize
        {
            get { return (int)(FileBytes.Length / 1024); }
        }

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <returns>返回文件的服务器访问url</returns>
        public string SaveAs()
        {
            string url = GetFileUrl(FileName,UploadDirectory);
            url=this.SaveAs(url);
            return url;
        }

        #region properties

        /// <summary>
        /// 自动生成用户名，默认为false
        /// </summary>
        public virtual bool AutoCreateFileName { get; set; }

        /// <summary>
        /// 充许上传文件扩展名集合,格式如：gif|jpg|bmp|png。如果不设置测不进行验证
        /// </summary>
        public virtual string FileExtensions 
        {
            get { return _fileExtensions ;}
            set { _fileExtensions = value ;}
        }

        /// <summary>
        /// 充许上传文件大小，单位KB，默认为Web.Config中配置的"UpFileSize"
        /// </summary>
        public virtual int MaxFileSize
        {
            get { return _upfileSize; }
            set { _upfileSize = value; }
        }

        /// <summary>
        /// 上传文件目录，默认类web.config中配置的"UploadDirectory" + /Temp/
        /// </summary>
        public virtual string UploadDirectory
        {
            get { return _uploadDirectory; }
            set { _uploadDirectory=value; }
        }

        #endregion


        private string GetFileUrl(string fileName, string upfileDirectoryPath)
        {
            if (AutoCreateFileName)
                fileName = Globals.RandomStr(DateTime.Now,3) + Path.GetExtension(fileName);

            Globals.DirectoryExists(upfileDirectoryPath, true);
            return upfileDirectoryPath + fileName;
        }

        /// <summary>
        /// 判断文件是否已经存在，如果存在则重命名文件
        /// </summary>
        /// <param name="fileUrl">要判断的文件</param>
        /// <returns>新文件名</returns>
        private string CheckFileHas(string fileUrl)
        {
            if (Globals.FileExists(fileUrl))
            {
                string extension = Path.GetExtension(fileUrl);
                fileUrl = fileUrl.Replace(extension, Globals.RandomStr(1) + extension);

                fileUrl = CheckFileHas(fileUrl);
            }

            return fileUrl;
        }
    }
}
